/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xiaopo.flying.puzzle.ext;

import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;

/**
 * RectF工具类
 *
 * @since 2021-07-30
 */
public class RectF extends RectFloat {
    private static final float CENTER = 0.5f;

    /**
     * 构造
     */
    public RectF() {
        super();
    }

    /**
     * 构造
     *
     * @param positiveInfinity
     * @param positiveInfinity1
     * @param negativeInfinity
     * @param negativeInfinity1
     */
    public RectF(float positiveInfinity, float positiveInfinity1, float negativeInfinity, float negativeInfinity1) {
        super(positiveInfinity, positiveInfinity1, negativeInfinity, negativeInfinity1);
    }

    /**
     * 构造
     *
     * @param rect
     */
    public RectF(Rect rect) {
        super(rect);
    }

    /**
     * 构造
     *
     * @param rectFloat
     */
    public RectF(RectFloat rectFloat) {
        super(rectFloat);
    }

    /**
     * set方法
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void set(float left, float top, float right, float bottom) {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }

    /**
     * sort方法
     *
     * @param r
     */
    public static void sort(RectFloat r) {
        if (r.left > r.right) {
            float temp = r.left;
            r.left = r.right;
            r.right = temp;
        }
        if (r.top > r.bottom) {
            float temp = r.top;
            r.top = r.bottom;
            r.bottom = temp;
        }
    }

    /**
     * contains
     *
     * @param r
     * @return boolean
     */
    public boolean contains(RectFloat r) {
        return this.left < this.right && this.top < this.bottom
            && left <= r.left && top <= r.top
            && right >= r.right && bottom >= r.bottom;
    }

    /**
     * contains
     *
     * @param xx
     * @param yy
     * @return boolean
     */
    public boolean contains(float xx, float yy) {
        return left < right && top < bottom
            && xx >= left && xx < right && yy >= top && yy < bottom;
    }

    /**
     * centerX
     *
     * @return final
     */
    public final float centerX() {
        return (left + right) * CENTER;
    }

    /**
     * centerY
     *
     * @return final
     */
    public final float centerY() {
        return (top + bottom) * CENTER;
    }
}
